package com.zl.tby.controller;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.bean.OptionTypeLevel;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyTypeLevel;
import com.zl.tby.service.ITbyTypeLevelService;
import com.zl.tby.vo.TbyTypeLevelBean;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 物种级别管理控制器
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/11 14:28
 */
@Valid
@Controller
@Mapping("/typeLevelInfo")
public class TbyTypeLevelController {

    @Inject
    private ITbyTypeLevelService tbyTypeLevelService;

    /**
     * 保存或新增面积
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/saveOrUpdate", method = MethodType.POST)
    public R<TbyTypeLevel> saveOrUpdate(@Validated TbyTypeLevel entity) {
        try {
            if (entity.getId() == null) {
                return tbyTypeLevelService.add(entity);
            }
            return tbyTypeLevelService.update(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyTypeLevelBean> get(@NotNull Long id) {
        TbyTypeLevelBean entity = this.tbyTypeLevelService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }


    @Mapping(value = "/restParents", method = MethodType.GET)
    public R<Void> restParents() {
        return this.tbyTypeLevelService.restParents();
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return tbyTypeLevelService.delete(id);
    }

    /**
     * 树形表格数据
     * @return 分页结果
     */
    @Mapping(value = "/treeTableList", method = MethodType.GET)
    public R<TbyPageResult<TbyTypeLevelBean>> treeTableList() {
        return R.ok(this.tbyTypeLevelService.treeTableList());
    }

    /**
     * 下拉选数据
     * @return 结果
     */
    @Mapping(value = "/options", method = MethodType.GET)
    public R<List<OptionTypeLevel>> options(String name) {
        return R.ok(this.tbyTypeLevelService.options(name));
    }

}
